#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim: set ts=4 sw=4 noet:
#
# $Id: RegexPlugin.py 2 2008-06-05 13:15:18Z wejradford $

"""
RegexPlugin.py
"""

__version__ = "$Revision: 2 $"
# $URL: http://yare-gedit-plugin.googlecode.com/svn/trunk/yare/RegexPlugin.py $

import gedit
import sys
import os
import re
import logging
from gtk import glade
from FindDialog import FindDialog
from PatternManager import PatternManager
# Expecting the .glade file to be in the same directory, just with a .glade extension.
GLADE_PATH = re.compile("\.pyc?$").sub(".glade", __file__)

class RegexPlugin(gedit.Plugin):
	DATA_TAGS = {"FindDialog": FindDialog, "PatternManager": PatternManager}
	def __init__(self):
		"""
		Read the glade file.
		"""
		super(RegexPlugin, self).__init__()
		if not os.path.isfile(GLADE_PATH):
			print >> sys.stderr, "Could not find glade path: %s" % GLADE_PATH
			# FIXME - Which exception...
			raise Exception
		else:
			try:
				self._gladeObject = glade.XML(GLADE_PATH)
			except RuntimeError, e:
				print >> sys.stderr, "Error loading glade file: %s" % GLADE_PATH	
		logging.basicConfig(level=logging.DEBUG)
	
	def activate(self, window):
		"""
		Initialise all objects and attach them to the window.
		"""
		for dataTag, cls in self.DATA_TAGS.iteritems():
			window.set_data(dataTag, cls(self, window, self._gladeObject))
	
	def deactivate(self, window):
		"""
		Stop and detach all objects from the window.
		"""
		for dataTag in self.DATA_TAGS.iterkeys():
			window.get_data(dataTag).stop()
			window.set_data(dataTag, None)
		
	def update_ui(self, window):
		"""
		Update all the objects.
		"""
		for dataTag in self.DATA_TAGS.iterkeys():
			window.get_data(dataTag).update()

# End of $URL: http://yare-gedit-plugin.googlecode.com/svn/trunk/yare/RegexPlugin.py $
